package com.raymond.cmpp.client.queue;



import com.raymond.cmpp.common.msg.BaseMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 打包处理队列
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-14 11:39
 */
public class PackBlockingQueue {
    private final BlockingQueue<BaseMessage> packTask = new LinkedBlockingQueue<>();

    public List<BaseMessage> poll(int size) {
        List<BaseMessage> baseMessages = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            BaseMessage poll = packTask.poll();
            if (poll == null) {
                return baseMessages;
            }
            baseMessages.add(poll);
        }
        return baseMessages;
    }

    public void add(BaseMessage baseMessage) {
        packTask.add(baseMessage);
    }

    public int size() {
        return packTask.size();
    }

    public synchronized void wait0(int timeout) throws InterruptedException {
        this.wait(timeout);
    }

    public synchronized void notify0() {
        this.notify();
    }

    public synchronized void notifyAll0() {
        this.notifyAll();
    }
}
